<!--
Name: database
author: xingyuelongchen
QQ    : 237956234
Date  : 2021-06-28
-->
<template>
  <div class="page">
    <mixForm v-model="formData" :fields="formFields" :inline="true" />
    <mixTable :data="tableData" :fields="tableFields" />
    <mixPage v-model="page" />
    <el-dialog title="备份数据" :visible.sync="show" width="500px">
      <mixForm v-model="formData" :fields="fields" />
      <div slot="footer">
        <el-button @click="add" :loading="loading" type="primary">开始备份</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { backups_add, backups_addOne, backups_del, backups_edit, backups_list, backups_reset, backups_resetOne, backups_tablelist } from "@/api";
export default {
  name: "database",
  data() {
    return {
      show: false,
      loading: false,
      formData: {},
      formFields: [
        {
          k: "button",
          option: [{ label: "备份数据", type: "primary", click: () => (this.show = true) }]
        }
      ],
      fields: [
        { label: "备份名称", prop: "name" },
        { label: "数据表", k: "select", prop: "tableName", placeholder: "为空则备份整个数据库", options: this.getTables }
      ],
      tableData: [],
      tableFields: [
        { label: "id", prop: "id" },
        { label: "备份名称", prop: "name" },
        { label: "文件名", prop: "filename" },
        { label: "时间", prop: "createTime", k: "date", format: "YYYY-MM-DD hh:mm:ss" },
        {
          label: "操作",
          k: "button",
          option: [
            { label: "修改", click: this.edit, type: "warning" },
            { label: "删除", click: this.del, type: "danger" },
            { label: "恢复", click: this.backupsreset, type: "success" }
          ]
        }
      ],
      page: { page: 1, size: 10, total: 0, event: this.getData }
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      let { page, size } = { ...this.page };
      let { data } = await backups_list({ page, size });
      this.tableData = data?.data?.data || [];
      this.page.total = data?.data?.total;
    },
    // 备份
    async add() {
      this.loading = true;
      let { name, tableName = "" } = this.formData;
      if (tableName) {
        await backups_addOne({ name, tableName });
      } else {
        await backups_add({ name });
      }
      this.getData();
      this.loading = false;
      this.show = false;
    },
    // 删除
    async del({ row }) {
      if (await this.$confirm("删除后不可恢复，是否确定？", "警告", { type: "warning" })) {
        await backups_del({ id: row.id });
        this.getData();
      }
    },
    async edit({ row }) {
      let { value: name } = await this.$prompt("请输入备份名称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /[\w\d\u4e00-\u9fa5]{2,20}/,
        inputErrorMessage: "请输入备份名称！",
        inputPlaceholder: row.name
      });
      await backups_edit({ id: row.id, name });
      this.getData();
    },
    async backupsreset({ row }) {
      if (await this.$confirm("此操作将覆盖现有数据，确定继续？", "警告", { type: "warning" })) {
        if (row.tableName && row.type == 1) {
          await backups_resetOne(row);
        } else {
          await backups_reset(row);
        }
      }
    },
    // 获取数据表名称
    async getTables() {
      let { data } = await backups_tablelist();
      let arr = data?.data || [];
      return arr.map((e) => {
        return { label: e, value: e };
      });
    }
  }
};
</script>
<style lang='scss' scoped>
</style>